import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/utils/cache/AppCahche.dart';

import '../../routes/app_pages.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class loginController extends BaseController {
  TextEditingController phoneController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  // TextEditingController yzmController = TextEditingController();
  var _isObscure = false.obs;

  set Obscure(value) => _isObscure.value = value;

  get Obscure => _isObscure.value;
  var _obj = ''.obs;

  set obj(value) => _obj.value = value;

  get obj => _obj.value;

  @override
  void onInit() {
    super.onInit();
    print('loginController');
  }

  void login() async {
    var phone = phoneController.text;
    var password = passwordController.text;
    // var yzm= yzmController.text;

    if (phone.isEmpty) {
      print('phoneController${Get.context == null}');
      SmartDialog.showToast("请输入手机号");
    } else if (password.isEmpty) {
      SmartDialog.showToast("请输入密码");
    }
    else {
      showLoading();
      var resp = await HttpManager.reqLogin(phone, password);
      if (resp.isSuccess() && resp.data != null) {
        print('login${resp.data?.token}');
        AppCache.setToken(resp.data!.token);
        reqUserInfo();
      }
      hideLoading();
    }
  }

  void reqUserInfo() async {
    showLoading();
    var resp = await HttpManager.reqUserInfo();
    if (resp.isSuccess() && resp.data != null) {
      AppCache.setUserInfo(resp.data!);
      Get.offAllNamed(Routes.HOME);
    }
    hideLoading();
  }

  void clickForgetPsw() {
    Get.toNamed(Routes.RESET_PASSWORD);
  }

  void clickRegister() {
    Get.toNamed(Routes.REGISTER);
  }
}
